// @ 516
// _(:з」∠)_

define([], function() {
    'use strict';

    let main =  document.querySelector("main.main"),
        leftMainView = `<div class="leftMain">
                                    <ul class="leftMain_nav"></ul>
                                    <div class="leftMain_cont"></div>
                                    <ul class="disEvent_setting"></ul>
                                    <ul class="meteorEvent_setting"></ul>
                                </div>`;
    main.insertAdjacentHTML("beforeEnd", leftMainView);
    
    // 获取左侧导航信息
    RST.basic( Path + "/data/basic/leftNav.json", "", function( result ) {
        
        if ( result.data.length < 1 ) {
            console.log("左侧菜单项为空！");
            return false;
        }

        let leftMain = main.querySelector(".leftMain"),
            navList = leftMain.querySelector(".leftMain_nav"),
            contList = leftMain.querySelector(".leftMain_cont"),
            navItem = ``,
            contItem = ``;

        // 循环信息
        for ( let x = 0; x < result.data.length; x++ ) {
            let data = result.data[x],
                navItemIcon = ``;

            navItemIcon = `<i class="fa ${ data.awesome }" aria-hidden="true"></i>`;
            if ( data.image != "" ) { navItemIcon = `<img src="${ Path + data.image }" /`; }

            navItem += `<li class="nav_item" title="${ data.title }"> ${ navItemIcon } </li>`;

            contItem += `<div class="cont_item ${ data.value }">
                                    <div class="item_head">
                                        <h>${ data.title }</h>
                                    </div>
                                    <div class="item_cont"></div>
                                </div>`;
        }
        navList.insertAdjacentHTML("beforeEnd", navItem);
        contList.insertAdjacentHTML("beforeEnd", contItem);

        // 事件处理
        let moduleNav = navList.querySelectorAll(".nav_item"),
            moduleCont = contList.querySelectorAll(".cont_item");

        for ( let x = 0; x < moduleNav.length; x++ ) {
            moduleNav[x].addEventListener( "click", function( e ) {
                MC.mapEvent.removeAllEvent( false );

                // 隐藏高级设置
                let disEvent = document.querySelector(".disaster_box .item_child_list span.select"),
                    meteorEvent = document.querySelector(".meteor_list .meteor_child_item .item_set.select");
                if ( disEvent ) disEvent.click();
                if ( meteorEvent ) meteorEvent.click();

                let flsd = document.querySelector(".farmlandStationDetilas");
                if ( flsd ) flsd.remove();
                let flsd_item = document.querySelector(".farmlandStation_item.select");
                if ( flsd_item ) flsd_item.removeClass("select");
                
                if ( moduleNav[x].hasClass("select") ) {
                    MC.mapEvent.removeAllEvent( false );
                    moduleNav[x].removeAllClass("select");

                    if ( contList.offsetLeft > 0 ) contList.removeClass("select");
                } else {
                    moduleNav[x].removeAllClass("select");
                    moduleNav[x].addClass("select");

                    if ( moduleNav[x].getAttribute("title") == "小气候站" ) {
                        require([ "left-station" ]);
                    }
                    
                    hideAllElmt( moduleCont );
                    moduleCont[x].show();

                    // 限制右侧工具面板使用
                    if ( moduleCont[x].hasClass("disaster") ) {
                        MC.mapToolEvent = true;
                    } else {
                        MC.mapToolEvent = false;
                    }

                    if ( contList.offsetLeft < 0 ) contList.addClass("select");
                }
            }, false);
        }

        setTimeout(function() {
            moduleNav[0].click();
        }, 500);
    });

    // 引入单项内容
    require([ "left-dis" ]);
    require([ "left-meteor" ]);
    require([ "left-systemSet" ]);

    // 绑定地图工具事件
    // bind map tools event
    let tools = document.querySelectorAll(".tools_item");
    MC.bindMapToolsEvent([
        [ tools[0], "任意点" ],
        [ tools[1], "灾害点" ],
        [ tools[2], "切换底图" ]
    ]);
});